package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

type CollegeLevel struct {
	Ctx iris.Context
}

type CollegeLevelCreate struct {
	Id   string `json:"id"`
	High string `json:"high"`
	Low  string `json:"low"`
}

type CollegeLevelListParam struct {
	PerPage int `json:"perPage"`
	CurPage int `json:"curPage"`
}

func (c *CollegeLevel) OptionsAll() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

// 获取所有院校分数段位
func (c *CollegeLevel) PostAll() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	collegeLevelList := make([]models.CollegeLevel, 0)
	err = db.Find(&collegeLevelList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取所有院校分数段位：%s", err.Error()), "")
	}

	for i, item := range collegeLevelList {
		collegeLevelList[i].Name = fmt.Sprintf("%d~%d", item.High, item.Low)
	}

	return utils.CommonRes(20000, "成功获取所有院校分数段位", collegeLevelList)
}

func (c *CollegeLevel) OptionsCreate() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

// 创建院校分数段位
func (c *CollegeLevel) PostCreate() interface{} {
	collegeLevelCreate := CollegeLevelCreate{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &collegeLevelCreate)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错：%s", err.Error()), "")
	}

	if len(collegeLevelCreate.High) <= 0 {
		return utils.CommonRes(1000, "请输入高分数", "")
	}
	if len(collegeLevelCreate.Low) <= 0 {
		return utils.CommonRes(1000, "请输入低分数", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	collegeLevel := new(models.CollegeLevel)
	collegeLevel.High = utils.Atoi(collegeLevelCreate.High)
	collegeLevel.Low = utils.Atoi(collegeLevelCreate.Low)
	_, err = db.InsertOne(collegeLevel)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *CollegeLevel) OptionsDetail() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

// 获取院校分数段位详情
func (c *CollegeLevel) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错：%s", err.Error()), "")
	}

	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "请提供院校分数段位id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	collegeLevel := new(models.CollegeLevel)
	ok, err := db.ID(editParam.Id).Get(collegeLevel)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败：%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", collegeLevel)
}

func (c *CollegeLevel) OptionsEdit() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

// 修改院校分数段位
func (c *CollegeLevel) PostEdit() interface{} {
	collegeLevelCreate := CollegeLevelCreate{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &collegeLevelCreate)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错：%s", err.Error()), "")
	}

	if len(collegeLevelCreate.Id) <= 0 {
		return utils.CommonRes(1000, "请提供院校分数段位id", "")
	}
	if len(collegeLevelCreate.High) <= 0 {
		return utils.CommonRes(1000, "请输入高分数", "")
	}
	if len(collegeLevelCreate.Low) <= 0 {
		return utils.CommonRes(1000, "请输入低分数", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"high": collegeLevelCreate.High,
		"low":  collegeLevelCreate.Low,
	}
	_, err = db.Table(new(models.CollegeLevel)).ID(collegeLevelCreate.Id).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *CollegeLevel) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

// 获取院校分数段位列表
func (c *CollegeLevel) PostList() interface{} {
	collegeLevelListParam := CollegeLevelListParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &collegeLevelListParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错：%s", err.Error()), "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, "无法连接服务器", "")
	}

	collegeLevelList := make([]models.CollegeLevel, 0)
	start := (collegeLevelListParam.CurPage - 1) * collegeLevelListParam.PerPage
	err = db.Limit(collegeLevelListParam.PerPage, start).Find(&collegeLevelList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取院校分数段位列表：%s", err.Error()), "")
	}

	total, err := db.Where("id > ?", 0).Count(new(models.CollegeLevel))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取院校分数段位列表：%s", err.Error()), "")
	}

	for i, item := range collegeLevelList {
		collegeLevelList[i].Name = fmt.Sprintf("%d~%d", item.High, item.Low)
	}

	data := map[string]interface{}{
		"total": total,
		"list":  collegeLevelList,
	}

	return utils.CommonRes(20000, "成功获取院校分数段位列表", data)
}
